package com.jt.yygh.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.yygh.common.result.Result;
import com.jt.yygh.common.result.ResultPage;
import com.jt.yygh.hospital.VO.EditHospitalVO;
import com.jt.yygh.hospital.VO.HospitalSetsVO;
import com.jt.yygh.hospital.entity.hosp.HospitalSet;
import com.jt.yygh.hospital.service.HospitalSetService;
import com.jt.yygh.hospital.utils.HospitalSetsExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author jiang tao
 * @date 2022/2/11
 **/
@RestController
@RequestMapping("/admin/hospital/hospitalSet/")
@Api("hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("getAllHospitalSet")
    @GetMapping("list")
    public ResultPage<HospitalSet> getAllHospitalSet(String searchName, int pageNum, int pageSize){
        Page<HospitalSet> pageInfo = hospitalSetService.getAllHospitalSetsByPage(searchName, pageNum, pageSize);
        return ResultPage.success(pageNum, pageSize, pageInfo.getTotal(), pageInfo.getRecords());
    }

    @ApiOperation("removeHospitalSet")
    @DeleteMapping("{id}")
    public Result<Boolean> removeHospitalSet(@PathVariable Long id){
        boolean res = hospitalSetService.removeById(id);
        if(!res){
            return Result.fail(201, "移除失败，其检查原因");
        }
        return Result.success("移除成功",res);
    }

//    @ApiOperation("条件查询分页")
//    @GetMapping("listByWrapper")
//    public Result<List<HospitalSet>> getHospitalSets(@RequestParam("hosname") String hosname){
//        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
//        wrapper.like("hosname", hosname);
//        return  Result.success(hospitalSetService.list(wrapper));
//    }

    @ApiOperation("添加医院设置")
    @PostMapping("addHospitalSets")
    public Result<Boolean> addHospitalSets(HospitalSetsVO hospitalSetsVO){
        boolean res = hospitalSetService.addHospitalSetsVO(hospitalSetsVO);
        if(!res){
            return Result.fail(201, "添加失败，其检查原因");
        }
       return Result.success("添加成功", res);
    }

    @ApiOperation("根据id得到医院设置详情")
    @GetMapping("/getHospitalSets")
    public Result<HospitalSetsVO> getHospitalSets(Long id){
       HospitalSet hospitalSet = hospitalSetService.getById(id);
       HospitalSetsVO hospitalSetsVO = new HospitalSetsVO();
        BeanUtils.copyProperties(hospitalSet, hospitalSetsVO);
        return Result.success(hospitalSetsVO);
    }

    @ApiOperation("根据id修改医院设置")
    @PutMapping("/editHospitalSetsById")
    public Result<Boolean> editHospitalSetsById(EditHospitalVO editHospitalVO){
        HospitalSet hospitalSet = new HospitalSet();
        BeanUtils.copyProperties(editHospitalVO,hospitalSet);
        return Result.success(hospitalSetService.updateById(hospitalSet));
    }

    @ApiOperation("批量删除医院设置")
    @DeleteMapping("/batchDeleteHospitalSets")
    public Result<Boolean> batchDeleteHospitalSets(@RequestParam("ids") List<Long> ids){
        return  Result.success(hospitalSetService.removeByIds(ids));
    }

    @ApiOperation("导出医院设置excel")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {

        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        HSSFWorkbook sheets = HospitalSetsExcel.employee2Excel(hospitalSetList);
        String fileName = "hospitalSets.xls";

        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename="
                + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));

        OutputStream out = response.getOutputStream();
        sheets.write(out);
        sheets.close();
    }

    //TODO 批量导入


}
